# -*- coding: utf-8 -*-

from datetime import datetime, date, timedelta
from odoo import models, fields, api, _
from odoo.exceptions import UserError, Warning
import random
import logging
_logger = logging.getLogger(__name__)


class Account_payable(models.Model):
    """应付款项"""
    _name = 'sale_sys.account_payable'

    work_state_selection = [
        ('draft', u'提现申请：待付款'),
        ('confirm', u'提现申请：已付'),
    ]

    user = fields.Many2one('res.partner', string=u'用户', states={'draft': [('readonly', False)]})
    sum = fields.Float(string=u'申请提现金额', states={'draft': [('readonly', False)]})
    apply_date = fields.Datetime(string=u'申请日期', readonly=True, default=fields.datetime.now())
    voucher = fields.Many2one('account.move', string=u'凭证', states={'draft': [('readonly', False)]})
    num = fields.Char(string=u'单号', required=False, copy=False, readonly=True, index=True)
    payment_date = fields.Datetime(string=u'付款日期', readonly=True)
    state = fields.Selection(work_state_selection, default='draft', string=u'状态',
                             readonly=True)

    @api.model
    def create(self, vals):
        """生成编号"""
        if not vals.get('num'):
            vals['num'] = self.env['ir.sequence'].next_by_code('sale_sys.account_payable') or '/'
        return super(Account_payable, self).create(vals)

    def do_check(self):
        """原：跳转到登记付款界面 现：按钮"""
        return {'type': 'ir.actions.act_window',
                'view_type': 'form',
                "view_mode": "form",
                'target': 'new',
                'context': {'num': str(self.num), 'total_amount': float(self.sum)},
                'res_model': 'wizard.registration_payment_wizard',
                'auto_refresh': 1,
                'multi': False,
                'flags': {'initial_mode': 'edit'}
                }
       
